<template >
   <el-dialog :title="title" :close-on-click-modal="false" :before-close="closeDialog" :visible.sync="visible" width="700px" append-to-body dialogDrag>
        <el-form ref="form" :inline="true" :model="form" class="form" :rules="rules" size="small" label-width="80px">
            <el-form-item  label="所属分类"  prop="pid">   
                <treeselect v-model="form.pid" :multiple="false" :options="treeData" :normalizer="normalizer" style="width:220px;height:32px" placeholder="选择所属分类"/>
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-radio v-for="(value,key) in statusDicts" :key="key" v-model="form.status" :label="value">{{ key }}</el-radio>
            </el-form-item>
            <el-form-item label="分类名称" prop="name"> 
                <el-input v-model="form.name" style="width:220px"></el-input>
            </el-form-item>
            <el-form-item label="排序权值" prop="sort"> 
                <el-input-number v-model="form.sort" controls-position="right" :min="0" :max="999"></el-input-number>
            </el-form-item>
            <el-form-item label="备注">
                <el-input type="textarea" v-model="form.remark" style="width: 534px;"></el-input>
            </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
            <el-button type="text"    @click="closeDialog">取消</el-button>
            <el-button type="primary" @click="doSubmit" :loading="loading">确认</el-button>
        </div>
    </el-dialog>
</template>
<script>
// import { listDeptTree } from "@/api/sysDept";
// import { listRole } from "@/api/sysRole";
import { queryTreeCategory,addCategory,editCategory,selCategory} from '@/api/category'
// import { Message } from "element-ui";
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
    inject:['reload'],
     components: {
        Treeselect
    },
    data() {
        return {
            rowId:'',
            title:'',
            visible: false,
            treeData:[],
            statusDicts:this.$global.status,
            loading:false,  //确认提交按钮加载中
            form: {
                pid:null,
                sort:0,
                name:'',
                status: 1,
                remark:''
            },
            rules:{
                name:      [{ required: true,message: '分类名称不能为空'}],
            }
        };
    },
    methods: {
        init(row,isAdd) {
            this.title = (isAdd ? '新增文章分类':'编辑文章分类')
            this.getData()
            !!row && (this.rowId = row.id) && this.findCategoryById(this.rowId)
            this.$nextTick(() => {
                this.visible = true
            })
        },
        closeDialog(){
            this.$refs['form'].resetFields();
            this.visible = false
            this.loading = false
            this.rowId = ''
            this.form = {pid:null, sort:0, name:'', status: 1,remark:''}
        },
        getData(){
            this.apiQueryTreeCategory()
        },
        findCategoryById(id){
            this.apiSelCategoryById(id)
        },
        doSubmit() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    this.loading = true
                    this.$confirm("确定?",'提示',{ type: 'warning'}).then( ()=>{
                        if(this.rowId){
                            this.apiEditCategory(this.form)
                        }else{
                            this.apiAddCategory(this.form)
                        }
                    }).catch(err=>{
                        this.loading =false
                    })
                }
            })
        },
        normalizer(node) {
            return {
                label: node.name,
                children: node.children,
                id:node.id
            }
        },
        //==========>请求api接口<==========//
        //查询文章分类树
        apiQueryTreeCategory(){
            //管理员查询接口
            queryTreeCategory().then(res=>{
                if(res.code ===200){
                    this.treeData    = res.data
                }
            }).catch((err)=>{
            })
        },
        apiSelCategoryById(id){
            selCategory(id).then(res=>{
                if(res.code === 200){
                    this.form =res.data
                }
            },error=>{
                this.$message.error(res.message)
            })
        },
        apiEditCategory(admin){
            editCategory(admin).then(res => {
                if(res.code === 200){
                    this.$message.success(res.message)
                    this.closeDialog()
                    this.reload()
                }
            },error =>{
                this.loading = false
                this.$message.error(res.message)
            })
        },
        apiAddCategory(admin){
            addCategory(admin).then(res => {
                if(res.code === 200){
                    this.$message.success(res.message)
                    this.closeDialog()
                    this.reload()
                }
            },error =>{
                this.loading = false
                this.$message.error(res.message)
            })
        }
        //==========>请求api接口<==========//
        // getData() {
        //     listDeptTree().then(res => {
        //         this.treeData = res.data;
        //     });
        //     listRole().then(res => {
        //         this.roles = res.data.list;
        //     });
        // },
        // normalizer(node) {
        //     return {
        //         label: node.name,
        //         children: node.children,
        //     }
        // },
    },
};
</script>
<style scoped>

</style>


